const $user_table = require('../models/usertable')
const MD5 = require('md5.js')
const NodeRSA = require('node-rsa');
const key = new NodeRSA({
    b: 512
});

module.exports = {
    register:
        /**
         * 注册用户
         * 
         * @param {string} mail 
         * @param {string} name 
         * @param {string} pass 
         * @param {Function} callback
         */
        function (mail, name, pass, callback) {
            $user_table.searchbyMail(mail, function (result) {
                if (result.length != 0)
                    callback(0, "mail has been register")
                else {
                    $user_table.createUser(name, mail, new MD5().update(pass).digest('hex'))
                    callback(1, null)
                }
            })
        },
    login:
        /**
         * 用户登陆
         * 
         * @param {string} mail 
         * @param {string} pass 
         * @param {Function} callback 
         */
        function (mail, pass, callback) {
            $user_table.searchbyMail(mail, function (result) {
                //校验是否存在用户
                if (result.length != 1) {
                    callback({
                        success: 0,
                        log: 'not found user'
                    })
                    return
                }
                var user = result[0]
                //校验密码（MD5加密时输入相同数据结果一致）
                if (user.user_pass != new MD5().update(pass).digest('hex')) {
                    callback({
                        success: 0,
                        log: 'wrong password'
                    })
                    return
                }
                //生成随机密钥
                var keys = ""
                for (var i = 0; i < 10; i++) {
                    keys += parseInt(Math.random() * 10)
                }

                //RSA token中包含的内容
                var text = JSON.stringify({
                    id: user.user_id,
                    keys: keys
                })

                //将随机密钥存入
                user.token = keys
                //设置有效时间10分钟
                var time = new Date(new Date().getTime() + (10 * 60 * 1000))
                user.token_over_time = time
                //RSA加密
                var token = key.encrypt(text, 'base64');
                //存入修改
                $user_table.updateById(user, function (statue) {
                    if (statue == 1) {
                        //修改成功，返回密钥结果
                        callback({
                            success: 1,
                            data: {
                                token: token,
                                token_over_time: time.toString()
                            }
                        })
                        return
                    } else {
                        callback({
                            success: 0,
                            log: 'unknow error'
                        })
                        return
                    }
                })


            })
        }
}